package com.peaksport.pkms.permit.controller;

import javax.ws.rs.core.MediaType;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.peaksport.framework.base.controller.BaseController;
import com.peaksport.framework.vo.SessionInfo;
import com.peaksport.pkms.oauth.vo.TokenVO;
import com.peaksport.pkms.permit.service.PKOAuthServcie;
import com.peaksport.pkms.permit.service.SSOService;

/**
 * 实现简易oAuth
 * 
 * @author jinbin_guo  2019年4月8日 上午9:21:08
 *
 */
@RestSchema(schemaId ="pkms-permit-pkOAuth")
@RequestMapping(value="pkOAuth", produces = MediaType.APPLICATION_JSON)
public class PKOAuthController extends BaseController {
	
	@Autowired
	private PKOAuthServcie pkOAuthService;
	@Autowired
	private SSOService ssoService;
	
	/**
	 * 为第三方系统注册调用token
	 * @param userNum
	 * @param password
	 * @param clientNum
	 * @param clientName
	 * @param clientDesc
	 * @return
	 */
	@GetMapping("register")
	public TokenVO register(@RequestParam String userNum,
						 @RequestParam String password,
						 @RequestParam String clientNum,
						 @RequestParam String clientName,
						 @RequestParam(required=false) String clientDesc) {
		ssoService.login(userNum, password);
		TokenVO tokenVO = pkOAuthService.register(userNum, clientNum, clientName, clientDesc);
		return tokenVO;
	}
	@GetMapping("unRegister")
	public boolean unRegister(@RequestParam String userNum,
						 @RequestParam String password,
						 @RequestParam String clientId) {
		ssoService.login(userNum, password);
		boolean b = pkOAuthService.unRegister(clientId);
		return b;
	}
	
	/**
	 * 重建token,将之前未过期的token,refresh_token设置为过期
	 * @param userId
	 * @param clientId
	 * @return
	 */
	@GetMapping("rebuildToken")
	public TokenVO rebuildToken(@RequestParam String userId,
						 @RequestParam String clientId) {
		TokenVO tokenVO =  pkOAuthService.rebuildToken(userId, clientId);
		return tokenVO;
	}
	
	/**
	 * 根据refreshToken重新刷新token,将未过期的token设置为过期
	 * @param refreshToken
	 * @param clientId
	 * @return
	 */
	@GetMapping("refreshToken")
	public TokenVO refreshToken(@RequestParam String refreshToken,
								@RequestParam String clientId) {
		TokenVO tokenVO =  pkOAuthService.refreshToken(refreshToken, clientId);
		return tokenVO;
	}
	
	/**
	 * 验证token的有效性
	 * @param token
	 * @return
	 */
	@GetMapping("vaildToken")
	public TokenVO vaildToken(@RequestParam String token) {
		TokenVO tokenVO = pkOAuthService.vaildToken(token);
		return tokenVO;
	}
	
	/**
	 * 验证客户端的URL接口权限
	 * @param clientId
	 * @param url
	 * @return
	 */
	@GetMapping("vaildPermit")
	public boolean vaildPermit(@RequestParam String clientId,
							   @RequestParam String url) {
		boolean b = pkOAuthService.vaildPermit(clientId, url);
		return b;
								   
	}
	
	/**
	 * 验证token的有效性+验证客户端的URL接口权限
	 * 实为接口vaildPermit+vaildToken组合
	 * @param token
	 * @param url
	 * @return
	 */
	@GetMapping("vaildOAuth")
	public TokenVO vaildOAuth(@RequestParam String token,
							 @RequestParam String url) {
		TokenVO tokenVO = pkOAuthService.vaildToken(token);
		pkOAuthService.vaildPermit(tokenVO.getClientId(), url);
		return tokenVO;
	}
	
	@GetMapping("removeExpireAllToken") 
	public String removeAllToken(@RequestParam String clientId){
		pkOAuthService.removeExpireAllToken(clientId);
		return "成功清除过期的token、refresh token";
	}
	
	@GetMapping("getContext")
	public String getContextByToken(@RequestParam String token) {
		SessionInfo contextInfo  = pkOAuthService.getContextByToken(token);
		return contextInfo.getSessionId();
	}
	
	
	
	
	
	
	
}
